package com.pattern.structure.proxy;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @author yangguanzan
 * @date 4/1/22
 * @description
 */
public class CglibMethodInterceptor implements MethodInterceptor {
    private Object target;
    private IAdvice advice;
    public CglibMethodInterceptor(Object target,IAdvice advice) {
        this.target = target;
        this.advice = advice;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy)
        throws Throwable {
        if(null!=this.target && null!=advice){
            advice.exec();
        }
        return method.invoke(target, args);
    }
}
